package com.easybuilder.core.schedultask.controller;


import com.easybuilder.core.schedultask.entity.TaskPO;
import com.easybuilder.core.schedultask.service.TaskService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sys/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    /**
     * @Author zhangxingshuai
     * @Description //TODO 添加一个任务
     * @Date 13:43 2021/4/6
     * @Param [task]
     * @return java.lang.String
     **/
    @PostMapping("/add")
    public String addTask(@RequestBody TaskPO task) {

        if (task != null) {
            taskService.addTask(task);
        }
        return "success";
    }

    /**
     * @Author zhangxingshuai
     * @Description //TODO 暂停一个定时任务
     * @Date 13:44 2021/4/6
     * @Param [task]
     * @return java.lang.String
     **/
    @PostMapping("/pause")
    public String pause(@RequestBody TaskPO task) {

        if (task != null) {
            taskService.pause(task);
        }
        return "success";
    }


    /**
     * @Author zhangxingshuai
     * @Description //TODO 恢复一个定时任务
     * @Date 13:44 2021/4/6
     * @Param [task]
     * @return java.lang.String
     **/
    @PostMapping("/resume")
    public String resume(@RequestBody TaskPO task) {

        if (task != null) {
            taskService.resume(task);
        }
        return "success";
    }


    /**
     * @Author zhangxingshuai
     * @Description //TODO 更新一个定时任务
     * @Date 13:44 2021/4/6
     * @Param [task]
     * @return java.lang.String
     **/
    @PostMapping("/update")
    public String update(@RequestBody TaskPO task) {

        if (task != null) {
            taskService.update(task);
        }

        return "success";
    }

    /**
     * @Author zhangxingshuai
     * @Description //TODO 立即执行一个定时任务
     * @Date 13:44 2021/4/6
     * @Param [task]
     * @return java.lang.String
     **/
    @PostMapping("/runNow")
    public String runNow(@RequestBody TaskPO task) {

        if (task != null) {
            try {
                taskService.runNow(task);
            } catch (SchedulerException e) {
                e.printStackTrace();
            }
        }

        return "success";
    }

    /**
     * @Author zhangxingshuai
     * @Description //TODO 丢弃一个定时任务
     * @Date 13:44 2021/4/6
     * @Param [task]
     * @return java.lang.String
     **/
    @PostMapping("/drop")
    public String drop(@RequestBody TaskPO task) {

        if (task != null) {
            taskService.drop(task);
        }

        return "success";
    }

}
